//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SwapForwardMappings.h"
using namespace Cephei::QL::Models::Marketmodels;
#include <gen/QL/Models/Marketmodels/CurveState.h>
#include <gen/QL/Models/Marketmodels/MarketModel.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::CSwapForwardMappings::CSwapForwardMappings (boost::shared_ptr<QuantLib::SwapForwardMappings>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSwapForwardMappings = NULL;
#endif
	_ppSwapForwardMappings = &childNative;
    
}
Cephei::QL::Models::Marketmodels::CSwapForwardMappings::CSwapForwardMappings (QuantLib::SwapForwardMappings& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phSwapForwardMappings = NULL;
#endif
	_ppSwapForwardMappings = new boost::shared_ptr<QuantLib::SwapForwardMappings> (&childNative);
    
    _SwapForwardMappingsOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::CSwapForwardMappings::CSwapForwardMappings (CSwapForwardMappings^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSwapForwardMappings = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSwapForwardMappings = new boost::shared_ptr<QuantLib::SwapForwardMappings> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::CSwapForwardMappings::CSwapForwardMappings (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSwapForwardMappings = NULL;
#endif
	if (!t->IsSubclassOf(CSwapForwardMappings::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::CSwapForwardMappings::CSwapForwardMappings (QuantLib::Handle<QuantLib::SwapForwardMappings>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSwapForwardMappings = &childNative;
	_ppSwapForwardMappings = &static_cast<boost::shared_ptr<QuantLib::SwapForwardMappings>>(childNative.currentLink());
    
    _SwapForwardMappingsOwner = owner;
}
Cephei::QL::Models::Marketmodels::CSwapForwardMappings::CSwapForwardMappings (QuantLib::Handle<QuantLib::SwapForwardMappings> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSwapForwardMappings = &childNative;
	_ppSwapForwardMappings = &static_cast<boost::shared_ptr<QuantLib::SwapForwardMappings>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::CSwapForwardMappings::CSwapForwardMappings (QuantLib::SwapForwardMappings childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSwapForwardMappings = NULL;
#endif
	_ppSwapForwardMappings = new boost::shared_ptr<QuantLib::SwapForwardMappings> (new QuantLib::SwapForwardMappings (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::CSwapForwardMappings::~CSwapForwardMappings ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppSwapForwardMappings != NULL)
    {
	    delete _ppSwapForwardMappings;
        _ppSwapForwardMappings = NULL;
    }
}
Cephei::QL::Models::Marketmodels::CSwapForwardMappings::!CSwapForwardMappings ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppSwapForwardMappings != NULL)
    {
	    delete _ppSwapForwardMappings;
    }
}
QuantLib::SwapForwardMappings& Cephei::QL::Models::Marketmodels::CSwapForwardMappings::GetReference ()
{
    if (_ppSwapForwardMappings == NULL) throw REFNEW NativeNullException ();
	return **_ppSwapForwardMappings;
}
boost::shared_ptr<QuantLib::SwapForwardMappings>& Cephei::QL::Models::Marketmodels::CSwapForwardMappings::GetShared ()
{
    if (_ppSwapForwardMappings == NULL) throw REFNEW NativeNullException ();
	return *_ppSwapForwardMappings;
}
QuantLib::SwapForwardMappings* Cephei::QL::Models::Marketmodels::CSwapForwardMappings::GetPointer ()
{
    if (_ppSwapForwardMappings == NULL) throw REFNEW NativeNullException ();
	return &**_ppSwapForwardMappings;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SwapForwardMappings>& Cephei::QL::Models::Marketmodels::CSwapForwardMappings::GetHandle ()
{
	if (_phSwapForwardMappings == NULL)
	{
		_phSwapForwardMappings = new Handle<QuantLib::SwapForwardMappings> (*_ppSwapForwardMappings);
	}
	return *_phSwapForwardMappings;
}
#endif
bool Cephei::QL::Models::Marketmodels::CSwapForwardMappings::HasNative () 
{
	return (_ppSwapForwardMappings != NULL);
}

Double Cephei::QL::Models::Marketmodels::CSwapForwardMappings::Annuity (Cephei::QL::Models::Marketmodels::ICurveState^ cs, UInt64 startIndex, UInt64 endIndex, UInt64 numeraireIndex)
{
    CCurveState^ _Ccs;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ccs = safe_cast<CCurveState^> (cs);
        _Ccs->Lock();
        QuantLib::CurveState& _cs = static_cast<QuantLib::CurveState&> (_Ccs->GetReference ()); 
        QuantLib::Size _startIndex = (QuantLib::Size)ValueHelper::Convert (startIndex); //a
        QuantLib::Size _endIndex = (QuantLib::Size)ValueHelper::Convert (endIndex); //a
        QuantLib::Size _numeraireIndex = (QuantLib::Size)ValueHelper::Convert (numeraireIndex); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSwapForwardMappings)->annuity ( _cs,  _startIndex,  _endIndex,  _numeraireIndex );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccs != nullptr) _Ccs->Unlock();
    }
}
Double Cephei::QL::Models::Marketmodels::CSwapForwardMappings::SwapDerivative (Cephei::QL::Models::Marketmodels::ICurveState^ cs, UInt64 startIndex, UInt64 endIndex, UInt64 forwardIndex)
{
    CCurveState^ _Ccs;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ccs = safe_cast<CCurveState^> (cs);
        _Ccs->Lock();
        QuantLib::CurveState& _cs = static_cast<QuantLib::CurveState&> (_Ccs->GetReference ()); 
        QuantLib::Size _startIndex = (QuantLib::Size)ValueHelper::Convert (startIndex); //a
        QuantLib::Size _endIndex = (QuantLib::Size)ValueHelper::Convert (endIndex); //a
        QuantLib::Size _forwardIndex = (QuantLib::Size)ValueHelper::Convert (forwardIndex); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSwapForwardMappings)->swapDerivative ( _cs,  _startIndex,  _endIndex,  _forwardIndex );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccs != nullptr) _Ccs->Unlock();
    }
}
Double Cephei::QL::Models::Marketmodels::CSwapForwardMappings::SwaptionImpliedVolatility (Cephei::QL::Models::Marketmodels::IMarketModel^ volStructure, UInt64 startIndex, UInt64 endIndex)
{
    CMarketModel^ _CvolStructure;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CvolStructure = safe_cast<CMarketModel^> (volStructure);
        _CvolStructure->Lock();
        QuantLib::MarketModel& _volStructure = static_cast<QuantLib::MarketModel&> (_CvolStructure->GetReference ()); 
        QuantLib::Size _startIndex = (QuantLib::Size)ValueHelper::Convert (startIndex); //a
        QuantLib::Size _endIndex = (QuantLib::Size)ValueHelper::Convert (endIndex); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSwapForwardMappings)->swaptionImpliedVolatility ( _volStructure,  _startIndex,  _endIndex );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CvolStructure != nullptr) _CvolStructure->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

